﻿using UnityEngine;
using System.Collections;
using System;
using com.gzc.ThreadLockEvent;

namespace SocialPoint.Examples.MVC {
    public class UguiTimerLabelViewPresenter : UguiLabelViewPresenter {

        #region U3D API

        // 注册AI移动棋子事件处理
        void OnEnable ( ) {
            InitEvent( );
        }

        void OnDisable ( ) {
            // 移除事件处理
            UnsubscribeEvent( );
        }

        void OnDestroy ( ) {
            // 移除事件处理
            UnsubscribeEvent( );
        }

        #endregion U3D API

        // 移除事件处理
        void UnsubscribeEvent ( ) {
            EventDispatcher.Instance( ).UnregistEventListener(UpdateTimerViewEvent.UPDATE_TIMER_VIEW_EVENT, OnUpdateTimerText);
            Debuger.Log("移除监听事件:" + UpdateTimerViewEvent.UPDATE_TIMER_VIEW_EVENT);
        }

        //监听事件
        void InitEvent ( ) {
            EventDispatcher.Instance( ).RegistEventListener(UpdateTimerViewEvent.UPDATE_TIMER_VIEW_EVENT, OnUpdateTimerText);
            Debuger.Log("监听事件:" + UpdateTimerViewEvent.UPDATE_TIMER_VIEW_EVENT);
        }

        void OnUpdateTimerText (EventBase eb) {
            UpdateTimerViewEvent updateTimerViewEvent = eb.eventValue as UpdateTimerViewEvent;
            //Debug.Log(string.Format("事件回调:{0}, timerText={1}", UpdateTimerViewEvent.UPDATE_TIMER_VIEW_EVENT, updateTimerViewEvent.timerText));

            this.SetGameTimerText(updateTimerViewEvent.timerText);
        }

        void SetGameTimerText (string gameTimerText) {
            if (!string.IsNullOrEmpty(gameTimerText)) {
                base.m_textLabel.text = gameTimerText;
            } else {
                Debug.LogError("gameTimerText is null or empty");
            }
        }

    }
}

